#!/bin/bash
if [ "$BRSRCDIR" == "" ]; then
 BRSRCDIR=../buildroot
fi

# Read secrets
if [ -f "secrets" ]; then
 . secrets
else
 echo "Secrets file not found, this isn't an official HiFiBerry build"
 echo "some components might not work as expected"
fi

cd `dirname $0`
MYDIR=`pwd`
TS=`date +%Y%m%d`
echo $TS > buildroot/VERSION

if [ "$1" == "" ]; then
 VERSION=`cat .piversion`
 echo No version given, assuming Pi$VERSION
else
 VERSION=$1
fi

KNOWNVERSION=`echo "2 3 4 0 02" | grep $VERSION`
if [ "$KNOWNVERSION" == "" ]; then
 echo "Unsupported version $VERSION, aborting"
 exit 1
fi

BRDIR=`./brdir $VERSION`
echo "Building in $BRDIR"

if [ -f $MYDIR/notify ]; then
 $MYDIR/notify begin
fi

# Remove some packages
if [ "$2" != "" ]; then
 echo Removing $BRDIR/build/$2*  $BRDIR/build/host-$2*
 rm -rf $BRDIR/build/$2*  $BRDIR/build/host-$2* $BRDIR/per-package/$2*
fi

if [ "$3" != "" ]; then
 echo Removing $BRDIR/build/$3*  $BRDIR/build/host-$3*
 rm -rf $BRDIR/build/$3*  $BRDIR/build/host-$3* $BRDIR/per-package/$3*
fi

cd $BRSRCDIR
mkdir -p $BRDIR/target/etc
echo $VERSION > $BRDIR/target/etc/piversion


make O=$BRDIR BR2_EXTERNAL=../hifiberry-os/buildroot olddefconfig

# Check if parallel build are supported
PARALLEL=`cat $BRDIR/.config | grep "BR2_PER_PACKAGE_DIRECTORIES=y"`
if [ "$NOPARALLEL" != "" ]; then
 PARALLEL=""
fi
if [ "$PARALLEL" == "" ]; then
 echo "Using standard (non package-parallel) build system"
 make O=$BRDIR BR2_EXTERNAL=../hifiberry-os/buildroot
else
 JOBS=`cat /proc/cpuinfo  | grep ^processor | wc -l`
 echo "Using new package-parallel build with $JOBS parallel jobs"
 make -j$JOBS O=$BRDIR BR2_EXTERNAL=../hifiberry-os/buildroot
fi

if [ -f $MYDIR/notify ]; then
 $MYDIR/notify finish
fi
